package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/cmd/rpc/pb"
	"mall/common/xerr"

	"mall/app/product/cmd/api/internal/svc"
	"mall/app/product/cmd/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetBrandListByPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetBrandListByPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBrandListByPageLogic {
	return &GetBrandListByPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetBrandListByPageLogic) GetBrandListByPage(req *types.GetBrandListByPageReq) (resp *types.GetBrandListByPageResp, err error) {
	list, err := l.svcCtx.ProductRpc.GetBrandListByPage(l.ctx, &pb.GetBrandListByPageReq{
		PageSize: req.PageSize,
		Offset:   req.Offset,
	})
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithMsg("获取失败"), "获取品牌失败 req: %+v, err: %v", req, err)
	}
	brandList := make([]types.Brand, len(list.BrandList))
	for i, v := range list.BrandList {
		brandList[i] = types.Brand{
			BrandId:     v.BrandId,
			Name:        v.Name,
			Descript:    v.Descript,
			FirstLetter: v.FirstLetter,
			Sort:        v.Sort,
			ShowStatus:  v.ShowStatus,
			Logo:        v.Logo,
		}
	}
	resp = &types.GetBrandListByPageResp{
		List: brandList,
	}
	return
}
